/*
 * Copyright ©2021 su binglun(9085309@qq.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *---------------------------------------------------------------------
 *-                                                                   -
 *-           Website : http://phoenix.sunny360.cn/                   -
 *-                                                                   -
 *---------------------------------------------------------------------
 */
package cn.sunny360.core.verticle;

import cn.sunny360.core.common.HttpHandlerMapping;
import cn.sunny360.core.common.PhRouter;
import cn.sunny360.core.common.RouterInterceptorChain;
import cn.sunny360.core.interceptor.ExtJsonRouterInterceptor;
import cn.sunny360.core.utils.Container;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;


/**
 * This verticle exposes a HTTP endpoint to process login service with REST
 * APIs.
 *
 * @author sublun
 */
public class RestApiVerticle extends HttpBaseVerticle {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void start() throws Exception {
		super.start();
		HttpHandlerMapping httpHandlerMapping = Container.getComponent("restHandlerMapping");
		PhRouter router = PhRouter.phRouter(vertx, httpHandlerMapping);
		//配置拦截器
		wrappingInterceptor(router);

		// get HTTP host and port from configuration, or use default value
		String host = config().getString("user.http.address", "0.0.0.0");
		int port = config().getInteger("user.http.port", 8802);

		// create HTTP server and publish REST service
		createHttpServer(router, host, port)
				.future()
				.onSuccess(server->{
					logger.info("Http rest api server start success.");
				})
				.onFailure(e->{
					e.printStackTrace();
					logger.error("Http rest api server create error!", e);
				});

	}
	private void wrappingInterceptor(PhRouter router) {
		RouterInterceptorChain routerInterceptorChain = RouterInterceptorChain.createNew();
		ExtJsonRouterInterceptor firstRouterInterceptor = new ExtJsonRouterInterceptor();
		routerInterceptorChain.add("/group/**", firstRouterInterceptor);
		router.setRouterInterceptorChain(routerInterceptorChain);
	}

}
